package org.belkodevelop.controller;

import org.belkodevelop.admin.service.AdminService;
import org.belkodevelop.controller.helper.AuthUtils;
import org.belkodevelop.user.service.UserService;
import org.dozer.DozerBeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/admin")
public class AdminCabinetController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private AuthUtils authUtils;

    @Autowired
    private DozerBeanMapper mapper;

    @Autowired
    private UserService userService;

    @RequestMapping({"/", "", "index.html"})
    public ModelAndView cabinet() {
        return new ModelAndView("redirect:/admin/events");
    }

    @RequestMapping("/confirmRegistration/{id}")
    public String confirmRegistration(@PathVariable Long id) {
        if (id != null) {
            adminService.confirmRegistration(id, authUtils.getCurrentSessionUser());
        }
        return "redirect:/admin";
    }

    @RequestMapping("/rejectRegistration/{id}")
    public String rejectRegistration(@PathVariable Long id) {
        if (id != null) {
            adminService.rejectRegistration(id, authUtils.getCurrentSessionUser());
        }
        return "redirect:/admin";
    }

    @RequestMapping("/removeDoctor/{id}")
    public String removeDoctor(@PathVariable Long id) {
        if (id != null) {
            adminService.removeUser(id, authUtils.getCurrentSessionUser());
        }
        return "redirect:/admin/doctors";
    }

    @RequestMapping("/removePatient/{id}")
    public String removePatient(@PathVariable Long id) {
        if (id != null) {
            adminService.removeUser(id, authUtils.getCurrentSessionUser());
        }
        return "redirect:/admin/patients";
    }

    @RequestMapping("/disableDoctor/{id}")
         public String disableDoctor(@PathVariable Long id) {
        if (id != null) {
            adminService.disableUser(id, authUtils.getCurrentSessionUser());
        }
        return "redirect:/admin/doctors";
    }

    @RequestMapping("/disablePatient/{id}")
    public String disablePatient(@PathVariable Long id) {
        if (id != null) {
            adminService.disableUser(id, authUtils.getCurrentSessionUser());
        }
        return "redirect:/admin/patients";
    }

    @RequestMapping("/enableDoctor/{id}")
    public String enableDoctor(@PathVariable Long id) {
        if (id != null) {
            adminService.enableUser(id, authUtils.getCurrentSessionUser());
        }
        return "redirect:/admin/doctors";
    }

    @RequestMapping("/enablePatient/{id}")
    public String enablePatient(@PathVariable Long id) {
        if (id != null) {
            adminService.enableUser(id, authUtils.getCurrentSessionUser());
        }
        return "redirect:/admin/patients";
    }


    @RequestMapping("/events")
    public ModelAndView events() {
        ModelAndView modelAndView = new ModelAndView("admin/events");
        return modelAndView;
    }

    @RequestMapping("/doctors")
    public ModelAndView doctors() {
        ModelAndView modelAndView = new ModelAndView("admin/doctors");
        modelAndView.addObject("doctors", userService.getAllDoctors());
        return modelAndView;
    }

    @RequestMapping("/patients")
    public ModelAndView patients() {
        ModelAndView modelAndView = new ModelAndView("admin/patients");
        modelAndView.addObject("patients", userService.getAllPatients());
        return modelAndView;
    }

}
